package blog.service.impl;

import blog.entity.Article;
import blog.entity.CateGory;
import blog.entity.Comment;
import blog.mapper.ArticleMapper;
import blog.mapper.CommentMapper;
import blog.service.CommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
@Service
public class CommentServiceImpl implements CommentService {

    @Resource
    private CommentMapper commentMapper;

    @Resource
    private ArticleMapper articleMapper;

    @Override
    public List<Comment> listRecentComment(Integer n) {
        List<Comment> commentList = commentMapper.listRecentComment(n);
        for (Comment c:commentList){
            Article article = articleMapper.getArticleById(c.getCommentArticleId());
            c.setArticle(article);
        }
        return commentList;
    }

    @Override
    public void deleteCommentById(Integer id) {
        commentMapper.deleteCommentById(id);
    }

    @Override
    public PageInfo<Comment> getPageArticleList(Integer pageIndex, Integer pageSize) {
        PageHelper.startPage(pageIndex,pageSize);
        List<Comment> commentList = commentMapper.getCommentList();
        for (Comment c:commentList){
            Article article = articleMapper.getArticleById(c.getCommentArticleId());
            c.setArticle(article);
        }
        return new PageInfo<>(commentList);
    }

    @Override
    public Comment getCommentById(Integer id) {
        return commentMapper.getCommentById(id);
    }

    @Override
    public void addComment(Comment comment) {
        commentMapper.addComment(comment);
    }

    @Override
    public void updateComment(Comment comment) {
        commentMapper.updateComment(comment);
    }

}
